﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFLocalizeExtension.Engine;

namespace LockCrypt {
    /// <summary>
    /// Allows configuration of shortcut/hotkeys.
    /// </summary>
    public partial class HotkeyMapper : UserControl {
        public static DependencyProperty ShortcutKeyProperty = DependencyProperty.Register("ShortcutKey", typeof(KeyGesture), typeof(HotkeyMapper), new PropertyMetadata(new KeyGesture(Key.None)));
        /// <summary>
        /// Gets or sets the shortcut key.
        /// </summary>
        /// <value>The shortcut key.</value>
        public KeyGesture ShortcutKey {
            get { return base.GetValue(ShortcutKeyProperty) as KeyGesture; }
            set { base.SetValue(ShortcutKeyProperty, value); }
        }

        public static DependencyProperty IsShortcutValidProperty = DependencyProperty.Register("IsShortcutValid", typeof(bool), typeof(HotkeyMapper), new PropertyMetadata(false));
        /// <summary>
        /// Gets or sets whether the shortcut key combination chosen is valid.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the shortcut key combination chosen is valid, otherwise <c>false</c>.
        /// </value>
        public bool IsShortcutValid {
            get { return Convert.ToBoolean(base.GetValue(IsShortcutValidProperty)); }
            set { base.SetValue(IsShortcutValidProperty, value); }
        }

        public HotkeyMapper() {
            InitializeComponent();
        }

        /// <summary>
        /// Checks whether the shortcut is valid when the control loads.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            IsShortcutValid = ShortcutKey.Key != Key.None;
        }

        /// <summary>
        /// Updates the chosen shortcut when the control state changes.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void chk_Changed(object sender, RoutedEventArgs e) {
            string requestedKey = txtKey.Text.Trim();
            if(requestedKey.Length != 1) {
                IsShortcutValid = false;
                return;
            }
            Key key = (Key)Enum.Parse(typeof(Key), requestedKey.ToUpperInvariant());
            ModifierKeys modifiers = ModifierKeys.None;
            if(chkCtrl.IsChecked == true) {
                modifiers |= ModifierKeys.Control;
            } else {
                modifiers &= ~ModifierKeys.Control;
            }
            if(chkShift.IsChecked == true) {
                modifiers |= ModifierKeys.Shift;
            } else {
                modifiers &= ~ModifierKeys.Shift;
            }
            if(chkAlt.IsChecked == true) {
                modifiers |= ModifierKeys.Alt;
            } else {
                modifiers &= ~ModifierKeys.Alt;
            }
            try {
                KeyGesture newGesture;
                if(modifiers == ModifierKeys.None) {
                    newGesture = new KeyGesture(key);
                } else {
                    newGesture = new KeyGesture(key, modifiers);
                }
                ShortcutKey = newGesture;
                IsShortcutValid = true;
            } catch {
                IsShortcutValid = false;
            }
        }
    }
}
